package com.org.base.util;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.view.Gravity;
import android.widget.Toast;

import com.org.zp.app.RecApp;


public class ConnectionChangeReceiver extends BroadcastReceiver {
	
	private NetworkInfo mobNetInfo = null;
	private NetworkInfo wifiNetInfo = null;

	@Override
	public void onReceive(Context context, Intent intent) {
		ConnectivityManager connectMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        
        try{
        	mobNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    		wifiNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        }catch(Exception e){
        	
        }
		
        if(null == mobNetInfo){
        	if (!wifiNetInfo.isConnected()) {
    			// unconnect network
    			((RecApp)context.getApplicationContext()).isNetWorkConnection=false;
    			Toast toast2 = Toast.makeText(context, "网络连接断开，请检查网络",
    					Toast.LENGTH_SHORT);
    			toast2.setGravity(Gravity.CENTER, 0, 0);
    			toast2.show();
    		} else {
    			((RecApp)context.getApplicationContext()).isNetWorkConnection=true;
    		}
        }else{
        	if (!mobNetInfo.isConnected() && !wifiNetInfo.isConnected()) {
    			// unconnect network
    			((RecApp)context.getApplicationContext()).isNetWorkConnection=false;
    			Toast toast2 = Toast.makeText(context, "网络连接断开，请检查网络",
    					Toast.LENGTH_SHORT);
    			toast2.setGravity(Gravity.CENTER, 0, 0);
    			toast2.show();
    		} else {
    			((RecApp)context.getApplicationContext()).isNetWorkConnection=true;
    		}
        }

		
	}

}
